<template>
  <!-- 包装物料配置表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">物料编号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.materialCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">物料名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.materialName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">产品包装规格（件/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.productPackagingSpecifications }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">器具分类</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.classificationInstruments }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">器具型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.instrumentModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">包装袋型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.packagingBagModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">隔板型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.partitionModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">隔板数量（张/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.partitionsNumber }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">打包带型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.packingBeltModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">打包带用量（公斤/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.packingBeltUsage }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">打包扣型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.packingBuckleModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">打包扣用量（个/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.packagingDeductionQuantity }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">拉伸膜型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.stretchFilmModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">拉伸膜用量（卷/托）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.stretchFilmDosage }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">木块型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.woodenBlockModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">木块用量（个/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.woodenBlockQuantity }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">干燥剂型号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.desiccantModel }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">干燥剂用量（包/箱）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.desiccantDosage }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="物料编号" prop="materialCode" label-width="80px">
            <el-input
              v-model="formData.materialCode"
              placeholder="请输入物料编号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料名称" prop="materialName" label-width="80px">
            <el-input
              v-model="formData.materialName"
              placeholder="请输入物料名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="产品包装规格（件/箱）"
            prop="productPackagingSpecifications"
            label-width="80px"
          >
            <el-input
              v-model="formData.productPackagingSpecifications"
              placeholder="请输入产品包装规格（件/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="器具分类"
            prop="classificationInstruments"
            label-width="80px"
          >
            <el-input
              v-model="formData.classificationInstruments"
              placeholder="请输入器具分类"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="器具型号"
            prop="instrumentModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.instrumentModel"
              placeholder="请输入器具型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="包装袋型号"
            prop="packagingBagModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.packagingBagModel"
              placeholder="请输入包装袋型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="隔板型号"
            prop="partitionModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.partitionModel"
              placeholder="请输入隔板型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="隔板数量（张/箱）"
            prop="partitionsNumber"
            label-width="80px"
          >
            <el-input
              v-model="formData.partitionsNumber"
              placeholder="请输入隔板数量（张/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="打包带型号"
            prop="packingBeltModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.packingBeltModel"
              placeholder="请输入打包带型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="打包带用量（公斤/箱）"
            prop="packingBeltUsage"
            label-width="80px"
          >
            <el-input
              v-model="formData.packingBeltUsage"
              placeholder="请输入打包带用量（公斤/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="打包扣型号"
            prop="packingBuckleModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.packingBuckleModel"
              placeholder="请输入打包扣型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="打包扣用量（个/箱）"
            prop="packagingDeductionQuantity"
            label-width="80px"
          >
            <el-input
              v-model="formData.packagingDeductionQuantity"
              placeholder="请输入打包扣用量（个/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="拉伸膜型号"
            prop="stretchFilmModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.stretchFilmModel"
              placeholder="请输入拉伸膜型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="拉伸膜用量（卷/托）"
            prop="stretchFilmDosage"
            label-width="80px"
          >
            <el-input
              v-model="formData.stretchFilmDosage"
              placeholder="请输入拉伸膜用量（卷/托）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="木块型号"
            prop="woodenBlockModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.woodenBlockModel"
              placeholder="请输入木块型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="木块用量（个/箱）"
            prop="woodenBlockQuantity"
            label-width="80px"
          >
            <el-input
              v-model="formData.woodenBlockQuantity"
              placeholder="请输入木块用量（个/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="干燥剂型号"
            prop="desiccantModel"
            label-width="80px"
          >
            <el-input
              v-model="formData.desiccantModel"
              placeholder="请输入干燥剂型号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="干燥剂用量（包/箱）"
            prop="desiccantDosage"
            label-width="80px"
          >
            <el-input
              v-model="formData.desiccantDosage"
              placeholder="请输入干燥剂用量（包/箱）"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESMATERIALPACKAGECONFIGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productPackagingSpecifications: [],
  classificationInstruments: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  instrumentModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  packagingBagModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  partitionModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  partitionsNumber: [],
  packingBeltModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  packingBeltUsage: [],
  packingBuckleModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  packagingDeductionQuantity: [],
  stretchFilmModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  stretchFilmDosage: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  woodenBlockModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  woodenBlockQuantity: [],
  desiccantModel: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  desiccantDosage: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  productPackagingSpecifications: "",
  classificationInstruments: "",
  instrumentModelId: "",
  instrumentModel: "",
  packagingBagModelId: "",
  packagingBagModel: "",
  partitionModelId: "",
  partitionModel: "",
  partitionsNumber: "",
  packingBeltModelId: "",
  packingBeltModel: "",
  packingBeltUsage: "",
  packingBuckleModelId: "",
  packingBuckleModel: "",
  packagingDeductionQuantity: "",
  stretchFilmModelId: "",
  stretchFilmModel: "",
  stretchFilmDosage: "",
  woodenBlockModelId: "",
  woodenBlockModel: "",
  woodenBlockQuantity: "",
  desiccantModelId: "",
  desiccantModel: "",
  desiccantDosage: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        materialId,
        materialCode,
        materialName,
        productPackagingSpecifications,
        classificationInstruments,
        instrumentModelId,
        instrumentModel,
        packagingBagModelId,
        packagingBagModel,
        partitionModelId,
        partitionModel,
        partitionsNumber,
        packingBeltModelId,
        packingBeltModel,
        packingBeltUsage,
        packingBuckleModelId,
        packingBuckleModel,
        packagingDeductionQuantity,
        stretchFilmModelId,
        stretchFilmModel,
        stretchFilmDosage,
        woodenBlockModelId,
        woodenBlockModel,
        woodenBlockQuantity,
        desiccantModelId,
        desiccantModel,
        desiccantDosage,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        materialId,
        materialCode,
        materialName,
        productPackagingSpecifications,
        classificationInstruments,
        instrumentModelId,
        instrumentModel,
        packagingBagModelId,
        packagingBagModel,
        partitionModelId,
        partitionModel,
        partitionsNumber,
        packingBeltModelId,
        packingBeltModel,
        packingBeltUsage,
        packingBuckleModelId,
        packingBuckleModel,
        packagingDeductionQuantity,
        stretchFilmModelId,
        stretchFilmModel,
        stretchFilmDosage,
        woodenBlockModelId,
        woodenBlockModel,
        woodenBlockQuantity,
        desiccantModelId,
        desiccantModel,
        desiccantDosage,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.productPackagingSpecifications = "";
  formData.classificationInstruments = "";
  formData.instrumentModelId = "";
  formData.instrumentModel = "";
  formData.packagingBagModelId = "";
  formData.packagingBagModel = "";
  formData.partitionModelId = "";
  formData.partitionModel = "";
  formData.partitionsNumber = "";
  formData.packingBeltModelId = "";
  formData.packingBeltModel = "";
  formData.packingBeltUsage = "";
  formData.packingBuckleModelId = "";
  formData.packingBuckleModel = "";
  formData.packagingDeductionQuantity = "";
  formData.stretchFilmModelId = "";
  formData.stretchFilmModel = "";
  formData.stretchFilmDosage = "";
  formData.woodenBlockModelId = "";
  formData.woodenBlockModel = "";
  formData.woodenBlockQuantity = "";
  formData.desiccantModelId = "";
  formData.desiccantModel = "";
  formData.desiccantDosage = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 包装物料配置表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesmaterialpackageconfig/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
